﻿using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository;
using System.Collections.Generic;
using System.Linq;
using log4net;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using System;
using System.Reflection;

namespace OpenAuth.WebApi.Areas.HisApi.Controllers
{
    [Route("HisApi/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private YinDBContext db;

        /// <summary>
        /// 注入构造函数
        /// </summary>
        /// <param name="_cggyw005App"></param>
        /// <param name="_cggyw009App"></param>
        public TestController( YinDBContext _db)
        {
            
            db = _db;


        }

        [HttpGet]
        public Response yin(string syxh)
        {
            DataTable SDF = null;
            if (syxh == "")
            {
                SDF = QueryOracle2DataTable("select * from  JHEMR.V_LEAVEHOS");
            }
            else
            {
                var cc = db.YIN_JHEMR.Where(p => p.SYXH == syxh).ToList().FirstOrDefault();
                if (cc != null)
                {
                    db.YIN_JHEMR.Remove(cc);
                    db.SaveChanges();
                }
                SDF = QueryOracle2DataTable("select * from  JHEMR.V_LEAVEHOS where SYXH='" + syxh + "'");
            }
            
            var ss = DtToList<YIN_JHEMR>(SDF);
            try
            {
                db.YIN_JHEMR.AddRange(ss);
                db.SaveChanges();
                var result = new Response<List<YIN_JHEMR>>();
                //查询医生信息
                result.Result = ss;
                result.Message = "插入成功";
                return result;
            }
            catch (Exception ex)
            {
                var result = new Response<List<YIN_JHEMR>>();
                //查询医生信息
                result.Result = ss;
                result.Message = "插入失败";
                return result;
                throw;
            }
        }
        [HttpGet]
        private DataTable QueryOracle2DataTable(string sql)
        {
            string data = string.Empty;
            DataTable OutDataTable = new DataTable();
            string constr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=200.200.200.9)(PORT=1521))(CONNECT_DATA=(SID=JHEMRV6N)));Persist Security Info=True;User ID=gsbx;Password=gsbx;";

            OracleConnection con = new OracleConnection(constr);//连接到数据库
            con.Open();
            string cmdText = sql;
            //创建SQL语句
            OracleCommand cmd = new OracleCommand(cmdText, con);
            //执行语句并返回结果
            OracleDataReader daReader = cmd.ExecuteReader();
            OutDataTable = new DataTable();
            DataRow dataRow;
            //设置Table名和Column名
            OutDataTable.TableName = "test";
            for (int j = 0; j < daReader.FieldCount; j++)
            {
                //获取列名
                OutDataTable.Columns.Add(daReader.GetName(j));
            }

            //循环取数据集合中的数据,存到DataTable中
            do
            {
                while (daReader.Read())
                {
                    dataRow = OutDataTable.NewRow();
                    for (int j = 0; j < daReader.FieldCount; j++)
                    {
                        data = daReader[j].ToString();
                        dataRow[j] = data;
                    }
                    OutDataTable.Rows.Add(dataRow);
                }
            } while (daReader.NextResult());

            return OutDataTable;

        }
        private List<T> DtToList<T>(DataTable dt) where T : class, new()
        {
            //属性列表
            List<System.Reflection.PropertyInfo> ls_pro = new List<System.Reflection.PropertyInfo>();

            Type t = typeof(T);
            //把T里所有public属性字段和dt列名一样的放进ls_pro属性列表存放
            Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) ls_pro.Add(p); });
            //Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.Contains(p.Name)) ls_pro.Add(p); });
            List<T> ls_t = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                T ob = new T();
                //遍历出属性列表里字段在dt里的值(非空即为有值)
                ls_pro.ForEach(p =>
                {
                    var sdf = dr[p.Name].GetType();
                    var ss = typeof(Decimal);
                    if (dr[p.Name] == DBNull.Value)
                    {
                        p.SetValue(ob, Convert.ToDecimal(dr[p.Name]), null);
                    }
                    if (dr[p.Name] != DBNull.Value)
                    {
                        p.SetValue(ob, dr[p.Name], null);
                    }
                });
                ls_t.Add(ob);
            }
            return ls_t;
        }


    }
}